package com.Lacia.emsserver.job;

import com.Lacia.emsserver.config.MqttConnection;
import com.Lacia.emsserver.config.ObjectMapperConfig;
import com.Lacia.emsserver.constant.TopicEmsConst;
import com.Lacia.emsserver.pojo.*;
import com.Lacia.emsserver.utils.GenData;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * @author 万里花
 * @date 2024/6/5 14:40
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DataPush {
    private final MqttConnection connection;


    /**
     * 模拟发送空调数据
     */

    @Scheduled(initialDelay = 5, fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
    public void airConditionPush() {
        try {
            var objectMapper = ObjectMapperConfig.getMapper();
            connection.publish("my/air/condition", objectMapper.writeValueAsString("time"+Instant.now().toEpochMilli()));
            log.info(LocalDateTime.now() +"=====发送my/air/condition");
        } catch (JsonProcessingException e) {
            log.error("publish device time error", e);
        }

    }


    /**
     * 模拟发送pcs数据
     */
    @Scheduled(initialDelay = 5, fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
    public void push() {
        try {
            var objectMapper = ObjectMapperConfig.getMapper();
            PcsPushData pcsPushData = GenData.generateRandomEmsData();
            connection.publish(TopicEmsConst.MY_PCS_PUSH_DATA, objectMapper.writeValueAsString(pcsPushData));
            log.info(System.currentTimeMillis() +"=====发送my/pcs/push1");
        } catch (JsonProcessingException e) {
            log.error("publish device time error", e);
        }

    }


    /**
     * 模拟发送Bms电池堆数据
     */
    @Scheduled(initialDelay = 5, fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
//    @Scheduled(initialDelay = 5, fixedDelay = 60, timeUnit = TimeUnit.SECONDS)
    public void bmsStackDataPush() {
        try {
            var objectMapper = ObjectMapperConfig.getMapper();
            BmsStackData bmsStackData = GenData.generateRandomBmsStackData();
            connection.publish(TopicEmsConst.MY_BMS_STACK_DATA, objectMapper.writeValueAsString(bmsStackData));
            log.info(System.currentTimeMillis() +"=====发送"+TopicEmsConst.MY_BMS_STACK_DATA);
        } catch (JsonProcessingException e) {
            log.error("publish device time error", e);
        }

    }

    /**
     * 模拟发送Bms电池簇数据
     */
    @Scheduled(initialDelay = 5, fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
    public void bmsClusterDataPush() {
        try {
            var objectMapper = ObjectMapperConfig.getMapper();
            BmsClusterMyData bmsClusterMyData = GenData.generateRandomBmsClusterMyData();
            connection.publish(TopicEmsConst.MY_BMS_CLUSTER_DATA, objectMapper.writeValueAsString(bmsClusterMyData));
            log.info(System.currentTimeMillis() +"=====发送"+TopicEmsConst.MY_BMS_CLUSTER_DATA);
        } catch (JsonProcessingException e) {
            log.error("publish device time error", e);
        }

    }


    /**
     * 模拟发送Bms电池簇单体数据
     */
    @Scheduled(initialDelay = 5, fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
    public void bmsClusterMonomerDataPush() {
        try {
            var objectMapper = ObjectMapperConfig.getMapper();
            BmsClusterMonomerData bmsClusterMonomerData = GenData.generateRandomBmsClusterMonomerData();
            connection.publish(TopicEmsConst.MY_BMS_CLUSTER_MONOMER_DATA, objectMapper.writeValueAsString(bmsClusterMonomerData));
            log.info(System.currentTimeMillis() +"=====发送"+TopicEmsConst.MY_BMS_CLUSTER_MONOMER_DATA);
        } catch (JsonProcessingException e) {
            log.error("publish device time error", e);
        }

    }

    /**
     * 模拟发送液冷数据
     */
    @Scheduled(initialDelay = 5, fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
    public void liqcoolDataPush() {
        try {
            var objectMapper = ObjectMapperConfig.getMapper();
            LiqcoolData liqcoolData = GenData.generateRandomLiqcoolData();
            connection.publish(TopicEmsConst.MY_LIQCOOL_DATA, objectMapper.writeValueAsString(liqcoolData));
            log.info(System.currentTimeMillis() +"=====发送"+TopicEmsConst.MY_LIQCOOL_DATA);
        } catch (JsonProcessingException e) {
            log.error("publish device time error", e);
        }

    }


    /**
     * 模拟发送计量电表数据
     */
    @Scheduled(initialDelay = 5, fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
    public void MeterMyDataPush() {
        try {
            var objectMapper = ObjectMapperConfig.getMapper();
            MeterMyData meterMyData = GenData.generateRandomMeterMyData();
            connection.publish(TopicEmsConst.MY_METER_DATA, objectMapper.writeValueAsString(meterMyData));
            log.info(System.currentTimeMillis() +"=====发送"+TopicEmsConst.MY_METER_DATA);
        } catch (JsonProcessingException e) {
            log.error("publish device time error", e);
        }

    }


    /**
     * 模拟告警数据，每分钟推送一次，故障概率1%,告警概率2%,异常状态下转正常概率10%
     */
//    @Scheduled(initialDelay = 5, fixedDelay = 60, timeUnit = TimeUnit.SECONDS)
//    public void alarmPush() {
//
//    }






}
